package net.lovesrc.allin.jobs;

import net.lovesrc.allin.jobs.base.BaseJob;
import net.lovesrc.support.service.generator.idcard.IDCard;
import net.lovesrc.support.service.generator.idcard.IDCardGeneratorService;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component
//@Profile("live")
public class FetchIdCardsJob extends BaseJob {

    @Autowired
    private IDCardGeneratorService idCardGeneratorService;

    @Override
    public void runOnce() {
        if (RandomUtils.nextInt(0, 100) == 50) {
            int count = jdbcTemplate.queryForObject("select count(1) from t_free_id_card", int.class);
            if (count >= 100 * 10000) {
                endJob("已获取到相应数量的数据");
            }
        }
        logger.info("开始执行插入身份证任务...");
        Set<IDCard> idCards = idCardGeneratorService.generate(50);
        for (IDCard idCard : idCards) {
            int count = jdbcTemplate.queryForObject("select count(1) from t_free_id_card where idCardNumber = ?", int.class, idCard.getNo());
            if (count == 0) {
                jdbcTemplate.update("insert into t_free_id_card(idCardNumber, realName, gender, address) values(?, ?, ?, ?)",
                        idCard.getNo(), idCard.getName(), idCard.getGender(), idCard.getAddr()
                );
            }
        }
    }


}
